import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store/index.js"

Vue.use(VueRouter)

//路由独享守卫 的封装
const fn = (url,next) => {
      //可以进入的地址
  let rootRouter = store.state.user.menus_url
  //如果去的地址是可以进入的地址 则放行
    if (rootRouter.includes(url)) {
      next()
    } else {
      next("/index")
    }
}

export const secondRouter = [{
  path: "menu",
    component: () => import("../page/menu/menu.vue"),
  meta: {
      title:"菜单管理"
  },
  // beforeEach: (to, from, next) => {
  //   //可以进入的地址
  //   let rootRouter = store.state.user.menus_url
  //   if (rootRouter.includes("/menu")) {
  //     next()
  //   } else {
  //     next("/index")
  //   }
  // }
  beforeEach: (to, from,next)=> {
    fn("/menu",next)
  }
},{
  path: "role",
  component:()=>import("../page/role/role.vue"),
  meta: {
      title:"角色管理"
  },
  beforeEach: (to, from,next)=> {
    fn("/role",next)
  }
},{
  path: "manage",
  component:()=>import("../page/manage/manage.vue"),
  meta: {
      title:"管理员管理"
  },
  beforeEach: (to, from,next)=> {
    fn("/manage",next)
  }
},{
  path: "cate",
  component:()=>import("../page/cate/cate.vue"),
  meta: {
      title:"商品分类"
  },
  beforeEach: (to, from,next)=> {
    fn("/cate",next)
  }
},{
  path: "specs",
  component:()=>import("../page/specs/specs.vue"),
  meta: {
      title:"商品规格"
  },
  beforeEach: (to, from,next)=> {
    fn("/specs",next)
  }
},{
  path:"goods",
  component:()=>import("../page/goods/goods.vue"),
  meta: {
      title:"商品管理"
  },
  beforeEach: (to, from,next)=> {
    fn("/goods",next)
  }
},{
  path:"member",
  component:()=>import("../page/member/member.vue"),
  meta: {
      title:"会员管理"
  },
  beforeEach: (to, from,next)=> {
    fn("/member",next)
  }
},{
  path:"banner",
  component:()=>import("../page/banner/banner.vue"),
  meta: {
      title:"轮播图管理"
  },
  beforeEach: (to, from,next)=> {
    fn("/banner",next)
  }
  }, {
    path: "seckill",
    component: () => import("../page/seckill/seckill.vue"),
    meta: {
      title: "秒杀活动"
  },
  beforeEach: (to, from,next)=> {
    fn("/seckill",next)
  }
  }]

const routes = [
  {
    path: '/',
    component: ()=>import("../page/login/login.vue")
  },
  {
    path: '/login',
    component: ()=>import("../page/login/login.vue")
  },
  {
    path: '/index',
    component: () => import("../page/index/index.vue"),
    children: [{
      path: "",
      component: () => import("../page/home/home.vue"),
    },...secondRouter
    ]
  },
  
]

const router = new VueRouter({
  routes
})

//路由前置守卫
// router.beforeEach((to, from, next)=> {
//   if (to.path == "/") {
//     next()
//     return
//   }
//   //判断是否登录过 （状态层里是否有用户信息 
//   if (store.state.user.token) {
//     next();
//     return
//   }
//   next("/")
// })


export default router
